package org.eden.heartbeat.core.report.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eden.heartbeat.api.model.MachineInfo;
import org.eden.heartbeat.api.model.Request;
import org.eden.heartbeat.core.persistence.RequestDAO;
import org.eden.heartbeat.core.report.ReportService;

import java.sql.SQLException;
import java.util.Date;

/**
 * Service that registers the status of a ping request to the database.
 *
 * @author trsimoes
 * @version $Revision:$
 */
public class ReportServiceImpl implements ReportService {

    private static Log LOG = LogFactory.getLog(ReportServiceImpl.class);

    private RequestDAO requestDAO;

    public ReportServiceImpl(RequestDAO requestDAO) {
        this.requestDAO = requestDAO;
    }

    @Override
    public void report(final MachineInfo machineInfo, final Date date, final boolean result) {

        Request request = new Request();
        request.setHost(machineInfo.getHost());
        request.setPort(machineInfo.getPort());
        request.setDate(date);
        request.setResult(result);

        try {
            requestDAO.insert(request);
        } catch (SQLException e) {
            if (LOG.isErrorEnabled()) {
                LOG.error("Report not written", e);
            }
        }
    }
}